<?php

/**
* AdminEditGuideTest.php
*
* Guid add test
*
* @author     Blazej Zieba <b@zieba.info>
* @copyright  2007 konekto.com
* @version    CVS: $Id:$
*/
require_once '../framework/config.php';

require_once 'simpletest/autorun.php';
require_once 'simpletest/web_tester.php';

require_once '../framework/AuthSingleton.php';



class  AdminEditGuideTest extends WebTestCase
{
    private $testName,$testDesc,$testNameNew,$testDescNew,$url;
    private $username = '';
    private $password = '';

    public function setUp() 
    {
        $this->username = md5( uniqid( rand(), true ) )."@pl.pl";
        $this->password = "zuza";

        $global = &PEAR::getStaticProperty('CONFIG_INI','GLOBAL');
        $this->url = $global['service_url'];
        $this->testName = md5(uniqid(rand(), true));
        $this->testDesc = md5(uniqid(rand(), true));
        $this->testNameNew = md5(uniqid(rand(), true));
        $this->testDescNew = md5(uniqid(rand(), true));
        $this->addHeader("User-Agent: Mozilla/5.0(SymbianOS/9.2; U; Series60/3.1 NokiaE51/1.00.000; Profile/MIDP-2.0 Configuration/CLDC-1.1;)AppleWebKit/413(KHTML,like Gecko)Safari/413"); 
        $this->get($this->url.'?cmd=AdminGuideList');

        AuthSingleton::instance()->addUser($this->username,$this->password);
        $this->setFieldByName('username', $this->username);
        $this->setFieldByName('password', $this->password);
        $this->click("Login");


    }

    public function testEditTextOnly()
    {
        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN');  
        $dbh = new PDO($options['dsn_string']); 
        $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );       
        $dbh->exec("INSERT INTO guide ( name, desc, owner ) VALUES ('".$this->testName."','".$this->testDesc."','".$this->username."')");
        $this->get($this->url.'?cmd=AdminAddGuide&id='.$dbh->lastInsertId());

        $this->assertTitle('Wirtualny Przewodnik Administracja');
        $this->assertField('name', $this->testName);
        $this->assertField('desc', $this->testDesc);
        $this->assertField('pic_file'); 
        $this->assertField('sound_file');
        $this->assertFalse( $this->setFieldByName("delete_pic_file", 1 ) );
        $this->assertFalse( $this->setFieldByName("delete_sound_file", 1 ) );  
        
        $this->assertTrue($this->setFieldByName('name', $this->testNameNew ));       
        $this->assertTrue($this->setFieldByName('desc', $this->testDescNew ));    

        $this->click("Zapisz");

        $this->assertText("Trasa zaktualizowana.");

        $sth = $dbh->prepare("SELECT COUNT(ROWID) FROM guide WHERE name = \"".$this->testNameNew."\" AND desc = \"".$this->testDescNew."\"");
        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_BOTH);
        $this->assertEqual($result[0],1);
           
        
    }

    public function testEditTextAndPic()
    {
        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN'); 
        $upload = &PEAR::getStaticProperty('CONFIG_INI','UPLOAD');
        $global = &PEAR::getStaticProperty('CONFIG_INI','GLOBAL');

        $dbh = new PDO($options['dsn_string']); 
        $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );       

        $dbh->exec("INSERT INTO guide ( name, desc,owner ) VALUES ('".$this->testName."','".$this->testDesc."','".$this->username."')");
        $lastInsertId = $dbh->lastInsertId();
        $dbh->exec("UPDATE guide SET  pic_file = '".$lastInsertId."images.jpg' WHERE rowid=".$lastInsertId);
        copy( dirname(__FILE__).'/images.jpg', $upload['upload_dir'].$lastInsertId.'images.jpg');
        
        $this->get($this->url.'?cmd=AdminAddGuide&id='.$lastInsertId);
        $this->assertTitle('Wirtualny Przewodnik Administracja');
        $this->assertField('name', $this->testName);
        $this->assertField('desc', $this->testDesc);
        $this->assertField('pic_file'); 
        $this->assertField('sound_file');
        $this->setFieldByName("delete_pic_file", 1 );
        $this->assertText($lastInsertId.'images.jpg'); 
        $this->assertPattern('/send_image.php\?id='.$lastInsertId.'images.jpg/');
        $this->assertFalse( $this->setFieldByName("delete_sound_file", 1 ) );  
        
        $this->assertTrue($this->setFieldByName('pic_file', dirname(__FILE__).'/imagesnew.jpg'));
        $this->assertTrue($this->setFieldByName('name', $this->testNameNew ));       
        $this->assertTrue($this->setFieldByName('desc', $this->testDescNew ));    

        $this->click("Zapisz");

        $this->assertText("Trasa zaktualizowana.");

        $sth = $dbh->prepare("SELECT COUNT(ROWID) FROM guide WHERE name = '".$this->testNameNew."' AND 
                                                                   desc = '".$this->testDescNew."' AND 
                                                                   pic_file = '".$lastInsertId."imagesnew.jpg'" );
        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_BOTH);
        $this->assertEqual($result[0],1);
        $this->assertTrue(is_file($upload['upload_dir'].$lastInsertId.'imagesnew.jpg'));

    }

    public function testEditTextAndSound()
    {
        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN'); 
        $upload = &PEAR::getStaticProperty('CONFIG_INI','UPLOAD');
        $global = &PEAR::getStaticProperty('CONFIG_INI','GLOBAL'); 

        $dbh = new PDO($options['dsn_string']); 
        $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );       

        $dbh->exec("INSERT INTO guide ( name, desc, owner ) VALUES ('".$this->testName."','".$this->testDesc."','".$this->username."')");
        $lastInsertId = $dbh->lastInsertId();
        $dbh->exec("UPDATE guide SET  sound_file = '".$lastInsertId."sound.wav' WHERE rowid=".$lastInsertId );
        copy( dirname(__FILE__).'/sound.wav', $upload['upload_dir'].$lastInsertId.'sound.wav');
        
        $this->get($this->url.'?cmd=AdminAddGuide&id='.$lastInsertId);
        $this->assertTitle('Wirtualny Przewodnik Administracja');
        $this->assertField('name', $this->testName);
        $this->assertField('desc', $this->testDesc);
        $this->assertField('pic_file'); 
        $this->assertField('sound_file');
        $this->setFieldByName("delete_sound_file", 1 );
        $this->assertLink('Pobierz dzwięk',$global['service_url'].'send_sound.php?id='.$lastInsertId.'sound.wav'); 
        $this->assertFalse( $this->setFieldByName("delete_pic_file", 1 ) );  
        

        $this->assertTrue($this->setFieldByName('sound_file', dirname(__FILE__).'/soundnew.wav'));
        $this->assertTrue($this->setFieldByName('name', $this->testNameNew ));       
        $this->assertTrue($this->setFieldByName('desc', $this->testDescNew ));    

        $this->click("Zapisz");

        $this->assertText("Trasa zaktualizowana.");

        $sth = $dbh->prepare("SELECT COUNT(ROWID) FROM guide WHERE name = '".$this->testNameNew."' AND 
                                                                   desc = '".$this->testDescNew."' AND 
                                                                   sound_file = '".$lastInsertId."soundnew.wav'" );
        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_BOTH);
        $this->assertEqual($result[0],1);
        $this->assertTrue(is_file($upload['upload_dir'].$lastInsertId.'soundnew.wav'));
 

    }

    public function testEditAll()
    {
        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN'); 
        $upload = &PEAR::getStaticProperty('CONFIG_INI','UPLOAD');
 
        $dbh = new PDO($options['dsn_string']); 
        $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );       

        $dbh->exec("INSERT INTO guide ( name, desc, owner ) VALUES ('".$this->testName."','".$this->testDesc."','".$this->username."')");
        $lastInsertId = $dbh->lastInsertId();

        $dbh->exec("UPDATE guide SET  sound_file = '".$lastInsertId."sound.wav' WHERE rowid=".$lastInsertId);
        copy( dirname(__FILE__).'/sound.wav', $upload['upload_dir'].$lastInsertId.'sound.wav');
        $dbh->exec("UPDATE guide SET  pic_file = '".$lastInsertId."images.jpg' WHERE rowid=".$lastInsertId);
        copy( dirname(__FILE__).'/images.jpg', $upload['upload_dir'].$lastInsertId.'images.jpg');
    
        $this->get($this->url.'?cmd=AdminAddGuide&id='.$lastInsertId);
        $this->assertTitle('Wirtualny Przewodnik Administracja');
        $this->assertField('name', $this->testName);
        $this->assertField('desc', $this->testDesc);
        $this->assertField('pic_file'); 
        $this->assertField('sound_file');
        $this->setFieldByName("delete_sound_file", 1 );
        $this->setFieldByName("delete_pic_file", 1 ); 

        $this->assertText($lastInsertId.'images.jpg');    
        $global = &PEAR::getStaticProperty('CONFIG_INI','GLOBAL'); 
        $this->assertLink('Pobierz dzwięk',$global['service_url'].'send_sound.php?id='.$lastInsertId.'sound.wav'); 

        

        $this->assertTrue($this->setFieldByName('sound_file', dirname(__FILE__).'/soundnew.wav'));
                $this->assertTrue($this->setFieldByName('pic_file', dirname(__FILE__).'/imagesnew.jpg'));
        $this->assertTrue($this->setFieldByName('name', $this->testNameNew ));       
        $this->assertTrue($this->setFieldByName('desc', $this->testDescNew ));    

        $this->click("Zapisz");

        $this->assertText("Trasa zaktualizowana.");

        $sth = $dbh->prepare("SELECT COUNT(ROWID) FROM guide WHERE name = '".$this->testNameNew."' AND 
                                                                   desc = '".$this->testDescNew."' AND 
                                                                   pic_file = '".$lastInsertId."imagesnew.jpg' AND
                                                                   sound_file = '".$lastInsertId."soundnew.wav'" );
        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_BOTH);
        $this->assertEqual($result[0],1);
        $this->assertTrue(is_file($upload['upload_dir'].$lastInsertId.'soundnew.wav'));
        $this->assertTrue(is_file($upload['upload_dir'].$lastInsertId.'imagesnew.jpg'));
 
 
    }
    
    public function testEditDeleteText()
    {

        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN');  
        $dbh = new PDO($options['dsn_string']); 
        $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );       
        $dbh->exec("INSERT INTO guide ( name, desc, owner ) VALUES ('".$this->testName."','".$this->testDesc."','".$this->username."')");
        $this->get($this->url.'?cmd=AdminAddGuide&id='.$dbh->lastInsertId());

        $this->assertTitle('Wirtualny Przewodnik Administracja');
        $this->assertField('name', $this->testName);
        $this->assertField('desc', $this->testDesc);
        $this->assertField('pic_file'); 
        $this->assertField('sound_file');
        $this->assertFalse( $this->setFieldByName("delete_pic_file", 1 ) );
        $this->assertFalse( $this->setFieldByName("delete_sound_file", 1 ) );  
        
        $this->assertTrue($this->setFieldByName('name', ''));       
        $this->assertTrue($this->setFieldByName('desc', '' ));    

        $this->click("Zapisz");

        $this->assertText("Podaj nazwę");
  
    }

    public function testEditDeletePic()
    {
        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN'); 
        $upload = &PEAR::getStaticProperty('CONFIG_INI','UPLOAD');
 
        $dbh = new PDO($options['dsn_string']); 
        $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );       

        $dbh->exec("INSERT INTO guide ( name, desc, owner ) VALUES ('".$this->testName."','".$this->testDesc."','".$this->username."')");
        $lastInsertId = $dbh->lastInsertId();
        $dbh->exec("UPDATE guide SET  pic_file = '".$lastInsertId."images.jpg' WHERE rowid=".$lastInsertId);
        copy( dirname(__FILE__).'/images.jpg', $upload['upload_dir'].$lastInsertId.'images.jpg');
        
        $this->get($this->url.'?cmd=AdminAddGuide&id='.$lastInsertId);
        $this->assertTitle('Wirtualny Przewodnik Administracja');
        $this->assertField('name', $this->testName);
        $this->assertField('desc', $this->testDesc);
        $this->assertField('pic_file'); 
        $this->assertField('sound_file');
        $this->setFieldByName("delete_pic_file", 1 );
        $this->assertText($lastInsertId.'images.jpg'); 
        $this->assertFalse( $this->setFieldByName("delete_sound_file", 1 ) );  
        

        $this->assertTrue($this->setFieldByName('name', $this->testNameNew ));       
        $this->assertTrue($this->setFieldByName('desc', $this->testDescNew ));    

        $this->click("Zapisz");

        $this->assertText("Trasa zaktualizowana.");

        $sth = $dbh->prepare("SELECT COUNT(ROWID) FROM guide WHERE name = '".$this->testNameNew."' AND 
                                                                   desc = '".$this->testDescNew."' AND 
                                                                   pic_file is null" );
        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_BOTH);
        $this->assertEqual($result[0],1);


        $this->assertFalse(is_file($upload['upload_dir'].$lastInsertId.'images.jpg'));
        $this->get($this->url.'?cmd=AdminAddGuide&id='.$lastInsertId);
        $this->assertNoText("Usuń zdjęcie");

    }

    public function testEditDeleteSound()
    {
        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN'); 
        $upload = &PEAR::getStaticProperty('CONFIG_INI','UPLOAD');
 
        $dbh = new PDO($options['dsn_string']); 
        $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );       

        $dbh->exec("INSERT INTO guide ( name, desc, owner ) VALUES ('".$this->testName."','".$this->testDesc."','".$this->username."')");
        $lastInsertId = $dbh->lastInsertId();
        $dbh->exec("UPDATE guide SET  sound_file = '".$lastInsertId."sound.wav' WHERE rowid=".$lastInsertId);
        copy( dirname(__FILE__).'/sound.wav', $upload['upload_dir'].$lastInsertId.'sound.wav');
        
        $this->get($this->url.'?cmd=AdminAddGuide&id='.$lastInsertId);
        $this->assertTitle('Wirtualny Przewodnik Administracja');
        $this->assertField('name', $this->testName);
        $this->assertField('desc', $this->testDesc);
        $this->assertField('sound_file'); 
        $this->assertField('pic_file');
        $global = &PEAR::getStaticProperty('CONFIG_INI','GLOBAL'); 
        $this->setFieldByName("delete_sound_file", 1 );
        $this->assertLink('Pobierz dzwięk',$global['service_url'].'send_sound.php?id='.$lastInsertId.'sound.wav'); ;
        $this->assertFalse( $this->setFieldByName("delete_pic_file", 1 ) );  
       
        $this->assertTrue($this->setFieldByName('name', $this->testNameNew ));       
        $this->assertTrue($this->setFieldByName('desc', $this->testDescNew ));    

        $this->click("Zapisz");

        $this->assertText("Trasa zaktualizowana.");

        $sth = $dbh->prepare("SELECT COUNT(ROWID) FROM guide WHERE name = '".$this->testNameNew."' AND 
                                                                   desc = '".$this->testDescNew."' AND 
                                                                   sound_file IS NULL" );
        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_BOTH);
        $this->assertEqual($result[0],1);



        $this->assertFalse(is_file($upload['upload_dir'].$lastInsertId.'sound.wav'));
        $this->get($this->url.'?cmd=AdminAddGuide&id='.$lastInsertId);
        $this->assertNoText("Usuń dźwięk");

    }

    public function testEditDeleteSoundPic()
    {
        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN'); 
        $upload = &PEAR::getStaticProperty('CONFIG_INI','UPLOAD');
 
        $dbh = new PDO($options['dsn_string']); 
        $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );       

        $dbh->exec("INSERT INTO guide ( name, desc, owner ) VALUES ('".$this->testName."','".$this->testDesc."','".$this->username."')");
        $lastInsertId = $dbh->lastInsertId();

        $dbh->exec("UPDATE guide SET  sound_file = '".$lastInsertId."sound.wav' WHERE rowid=".$lastInsertId);
        copy( dirname(__FILE__).'/sound.wav', $upload['upload_dir'].$lastInsertId.'sound.wav');
        $dbh->exec("UPDATE guide SET  pic_file = '".$lastInsertId."images.jpg' WHERE rowid=".$lastInsertId);
        copy( dirname(__FILE__).'/images.jpg', $upload['upload_dir'].$lastInsertId.'images.jpg');
    
        $this->get($this->url.'?cmd=AdminAddGuide&id='.$lastInsertId);
        $this->assertTitle('Wirtualny Przewodnik Administracja');
        $this->assertField('name', $this->testName);
        $this->assertField('desc', $this->testDesc);
        $this->assertField('pic_file'); 
        $this->assertField('sound_file');
        $this->setFieldByName("delete_sound_file", 1 );
        $this->setFieldByName("delete_pic_file", 1 ); 
        
        $this->assertText($lastInsertId.'images.jpg');   
        $global = &PEAR::getStaticProperty('CONFIG_INI','GLOBAL');  
        $this->assertLink('Pobierz dzwięk',$global['service_url'].'send_sound.php?id='.$lastInsertId.'sound.wav'); 

        $this->assertTrue($this->setFieldByName('name', $this->testNameNew ));       
        $this->assertTrue($this->setFieldByName('desc', $this->testDescNew ));    

        $this->click("Zapisz");

        $this->assertText("Trasa zaktualizowana.");

        $sth = $dbh->prepare("SELECT COUNT(ROWID) FROM guide WHERE name = '".$this->testNameNew."' AND 
                                                                   desc = '".$this->testDescNew."' AND 
                                                                   pic_file IS NULL AND
                                                                   sound_file IS NULL" );
        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_BOTH);
        $this->assertEqual($result[0],1);




        $this->assertFalse(is_file($upload['upload_dir'].$lastInsertId.'sound.wav'));
        $this->assertFalse(is_file($upload['upload_dir'].$lastInsertId.'images.jpg'));
 
 
    }

   public function testFileSizeSet()
    {
        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN');  
        $dbh = new PDO($options['dsn_string']); 
        $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );       
        $dbh->exec("INSERT INTO guide ( name, desc ) VALUES ('".$this->testName."','".$this->testDesc."')");
        $this->get($this->url.'?cmd=AdminAddGuide&id='.$dbh->lastInsertId());
        $this->assertTitle('Wirtualny Przewodnik Administracja');
        $this->assertField('MAX_FILE_SIZE',"2097152");
    }

    public function tearDown() 
    {
        
        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN');  
        $upload = &PEAR::getStaticProperty('CONFIG_INI','UPLOAD');
  
        $dbh = new PDO($options['dsn_string']);        
        $dbh->exec("DELETE FROM guide WHERE name = \"".$this->testName."\" AND desc = \"".$this->testDesc."\"");
        $sth = $dbh->prepare("SELECT pic_file,sound_file FROM guide WHERE name = \"".$this->testNameNew."\" AND desc = \"".$this->testDescNew."\"");
        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_ASSOC);
        
        
        if ( is_file($upload['upload_dir'].$result['pic_file']) )
        {
            unlink($upload['upload_dir'].$result['pic_file']);
        }

        if ( is_file($upload['upload_dir'].$result['sound_file']) )
        {
            unlink($upload['upload_dir'].$result['sound_file']);
        }

        $sth = $dbh->prepare("DELETE FROM guide WHERE name = \"".$this->testNameNew."\" AND desc = \"".$this->testDescNew."\"");
        $sth->execute();

        AuthSingleton::instance()->removeUser($this->username);
        $this->get($this->url.'?cmd=AdminGuideList&logout=1');
        
    }
    
}
?>
